**************************************************
*"Stakeholder Orientation, Product Market Competition, and the Cost of Equity"
* by Zhihong Chen, Sichen Shen, and Hong Zou
* Code for Data Processing and Analysis (STATA 17)

clear 
set more off
set matsize 1000	

**************************************************
**************************************************

foreach x in outreg2 reghdfe coefplot logout efolder {
capture which `x'
if _rc != 0 {
	ssc install `x'
}
}

***** SET WORKING DIRECTORY *****
global directory    "C:\CS project\CS data_code_table\RCFS_code_data" 
global path			"$directory/data/"

efolder, cd($directory/output)
global output		"$directory/output/"

cd "$path"
**************************************************
**************************************************

***** PREPARE DATASET FOR ANALYSIS *****			
        use cocsample, clear	
	    drop if avg_hhi_sic2==. 	
		gen treat=(list_scs ~=.)
		gen scs=(year >=list_scs)
		replace scs=0 if year >=alist_scs & year <blist_scs & alist_scs~=.
		gen csa=(year >= list_csa)
		gen bc=(year >= list_bc)
		gen fp=(year >= list_fp)
		gen pp=(year >= list_pp)
		gen antilaw2=(csa==1|bc==1|fp==1|pp==1)	
		gen firstlaw=(year >=firstat)
		replace firstlaw=0 if year >afirstat & afirstat~=.		
		save _rgls1,replace
		
*** setup the cohort window, [-10 10] for each event		
		foreach event_time in 1984 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007 {
		use _rgls1,replace				
		keep gvkey year avg_hhi_sic2 list_scs blist_scs state_incorp 
		keep if year==`event_time' & (list_scs==. | list_scs ==`event_time'| blist_scs==`event_time')
		gen pref10 =year-10
		gen postf10 =year+10 if state_incorp~="NE"                                         
		replace postf10 =year+6 if state_incorp=="NE" & `event_time'==1988  // The SCS law in NE was adopted in 1988, repealed in 1995, and readopted in 2007.
		drop blist_scs
		save _event`event_time'_rgls,replace
		}		
		use _event1984_rgls, clear
		foreach event_time in 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007{
		append using _event`event_time'_rgls
		}
		save _allevent_rgls ,replace
		
		use _allevent_rgls,clear
		by year, sort: egen median_cvari=pctile(avg_hhi_sic2), p(33)  // Divided firms into the high and low HHI group within each cohort
	    gen h_avg_hhi_sic2=0 
	    replace h_avg_hhi_sic2=1 if avg_hhi_sic2 >= median_cvari
		gen l_avg_hhi_sic2=0 
	    replace l_avg_hhi_sic2=1 if avg_hhi_sic2 < median_cvari
		keep gvkey year pref10 postf10 h_avg_hhi_sic2 l_avg_hhi_sic2 state_incorp avg_hhi_sic2 list_scs
	    rename year cohort
		save _cohortevent_rgls0,replace
		drop list_scs
		save _cohortevent_rgls,replace
				
		use _rgls1, clear
		joinby gvkey state_incorp using _cohortevent_rgls
		replace cohort=. if year <pref10
		replace cohort=. if year >postf10
		keep if cohort~=.
		save _rgls2, replace

        use _rgls2,clear
		keep rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls state_incorp gvkey cohort year scs h_avg_hhi_sic2 l_avg_hhi_sic2 lag_HQlngdp_pop lag_HQgdp_growth mf firstlaw antilaw2 treat
		drop if ubeta==.| idvol==.| logmv==.| mb==.| mlev==.| fbias==.| sgrw==. |ixrgls==.	
		save _coc_cohort_sample,replace
				
**************************************************
*** Table2&3 Summary stat. & baseline results
**************************************************	
		use _coc_cohort_sample, clear		
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"					
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls lag_HQlngdp_pop lag_HQgdp_growth {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)
		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2
		
		logout, save($output/Table2) excel replace: tabstat scs scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls , stat(n mean sd p25 p50 p75) labelwidth(32)
				
		reghdfe rgls scs `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		outreg2 using "$output/Table3.xls", keep (scs `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full)) addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append
        
		reghdfe rgls scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table3.xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append		

***************************************************
*** Table4 Dynamics of the effects of SCS law
***************************************************	
        use _coc_cohort_sample, clear		
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"					
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls  {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)

		local dy_var1= "scs_yb2_low scs_yb1_low scs_yb0_low scs_y1_low scs_y2_low scs_y3_low scs_y4_low scs_y5a_low"
		local dy_var2= "scs_yb2_high scs_yb1_high scs_yb0_high scs_y1_high scs_y2_high scs_y3_high scs_y4_high scs_y5a_high"
		
		gen yb2=( year == cohort-2)
		gen yb1=( year == cohort-1)
		gen yb0=( year == cohort)
		gen y1=( year == cohort+1)
		gen y2=( year == cohort+2)
		gen y3=( year == cohort+3)
		gen y4=( year == cohort+4)
		gen y5a=( year >= cohort+5)
		
		foreach a in yb2 yb1 yb0 y1 y2 y3 y4 y5a{
		gen scs_`a'_low=`a'*treat*l_avg_hhi_sic2
		gen scs_`a'_high=`a'*treat*h_avg_hhi_sic2
		}
		
		reghdfe rgls `dy_var1' `dy_var2' `control',absorb(firmn yearn) vce(cluster incorpn#cohort )
		outreg2 using "$output/Table4_dy.xls", keep (`dy_var1' `dy_var2' `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full)) addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append

***************************************************
*** Figure1 Dynamics of the effects of SCS law
***************************************************	
*** graph - high HHI group	    	
		lincom scs_yb2_high,level(95)
		matrix yb2=r(estimate), r(ub), r(lb) 
		matrix rownames yb2=yb2
		
		lincom scs_yb1_high,level(95)
		matrix yb1=r(estimate), r(ub) , r(lb) 
		matrix rownames yb1=yb1
		
		lincom scs_yb0_high,level(95)
		matrix y0=r(estimate), r(ub) , r(lb) 
		matrix rownames y0=y0
		
		lincom scs_y1_high,level(95)
		matrix y1=r(estimate), r(ub) , r(lb) 
		matrix rownames y1=y1
		
		lincom scs_y2_high,level(95)
		matrix y2=r(estimate), r(ub) , r(lb) 
		matrix rownames y2=y2
		
		lincom scs_y3_high,level(95)
		matrix y3=r(estimate), r(ub) , r(lb) 
		matrix rownames y3=y3
		
		lincom scs_y4_high,level(95)
		matrix y4=r(estimate), r(ub) , r(lb) 
		matrix rownames y4=y4
		
		lincom scs_y5a_high,level(95)
		matrix y5=r(estimate), r(ub) , r(lb) 
		matrix rownames y5=y5	
		matrix yb3=0,0,0
		matrix rownames yb3=yb3
		matrix G_high=yb3\yb2\yb1\y0\y1\y2\y3\y4\y5
		matrix colnames G_high= estimate ub lb
		matrix G_high=G_high'
		
		coefplot matrix(G_high[1]), ci((G_high[3] G_high[2])) vertical ciopts(lp(dash) recast(rcap)) yline(0) xline(4,lp(dash)) rename(yb3=≤-3 yb2=-2 yb1=-1 y0=0 y1=+1 y2=+2 y3=+3 y4=+4 y5=≥+5) graphr(c(gs16) ic(gs16)) name(Baseline_withCtrl,replace) xtitle("Event year",size(medium) color(black)) 
		graph export "$output/nDynamic high HHI.wmf", replace
		graph export "$output/nDynamic high HHI.pdf", as(pdf) replace

*** graph - low HHI group		    	
		lincom scs_yb2_low,level(95)
		matrix yb2=r(estimate), r(ub), r(lb) 
		matrix rownames yb2=yb2
		
		lincom scs_yb1_low,level(95)
		matrix yb1=r(estimate), r(ub) , r(lb) 
		matrix rownames yb1=yb1
		
		lincom scs_yb0_low,level(95)
		matrix y0=r(estimate), r(ub) , r(lb) 
		matrix rownames y0=y0
		
		lincom scs_y1_low,level(95)
		matrix y1=r(estimate), r(ub) , r(lb) 
		matrix rownames y1=y1
		
		lincom scs_y2_low,level(95)
		matrix y2=r(estimate), r(ub) , r(lb) 
		matrix rownames y2=y2
		
		lincom scs_y3_low,level(95)
		matrix y3=r(estimate), r(ub) , r(lb) 
		matrix rownames y3=y3
		
		lincom scs_y4_low,level(95)
		matrix y4=r(estimate), r(ub) , r(lb) 
		matrix rownames y4=y4
		
		lincom scs_y5a_low,level(95)
		matrix y5=r(estimate), r(ub) , r(lb) 
		matrix rownames y5=y5
			
		matrix yb3=0,0,0
		matrix rownames yb3=yb3
		matrix G_low=yb3\yb2\yb1\y0\y1\y2\y3\y4\y5
		matrix colnames G_low= estimate ub lb
		matrix G_low=G_low'
		
		coefplot matrix(G_low[1]), ci((G_low[3] G_low[2])) vertical ciopts(lp(dash) recast(rcap)) yline(0) xline(4,lp(dash)) rename(yb3=≤-3 yb2=-2 yb1=-1 y0=0 y1=+1 y2=+2 y3=+3 y4=+4 y5=≥+5) graphr(c(gs16) ic(gs16)) name(Baseline_withCtrl,replace) xtitle("Event year",size(medium) color(black)) 
		graph export "$output/nDynamic low HHI.wmf", replace
		graph export "$output/nDynamic low HHI.pdf", as(pdf) replace		
		
***************************************************
*** Table5 The effects of SCS law adoption on cash flow
***************************************************		
        use roa_cohort_sample,clear		
		local control = "lag_logat lag_mlev lag_rd lag_capex lag_adv lag_ppe lag_sgrw ixroa" 	
		qui foreach x in roa `control' { 
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}													
		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)	
		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2

		reghdfe roa scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control' ,absorb(firmn yearn) vce(cluster incorpn#cohort )
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table5.xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(roa-full) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append		
		
***************************************************
*** Table6 The effects of SCS law on the sensitivity of operating performance
***************************************************
        use fdroa_cohort_sample, clear
		local control = "lag_logat lag_mlev lag_rd lag_capex lag_adv lag_ppe lag_sgrw" 			
		qui foreach x in fdroa lag_logat lag_mlev lag_rd lag_capex lag_adv lag_ppe lag_sgrw {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}													
		egen incorpn = group(state_incorp) 
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)	
		gen scs_shockvavefroap=scs*shockvavefroap
				
		reghdfe fdroa scs scs_shockvavefroap shockvavefroap `control' if h_avg_hhi_sic2==0,absorb(firmn yearn) vce(cluster incorpn#cohort )
		local coeff=_b[shockvavefroap]+_b[scs_shockvavefroap]		
		test scs_shockvavefroap+shockvavefroap=0
		local F1=r(p)
		outreg2 using "$output/Table6.xls", keep (scs scs_shockvavefroap shockvavefroap  `control') stats(coef se) ctitle(Low avg_hhi_sic2) adjr2 adds(N_full, e(N_full), b2+b3,`coeff', F-stat p-value, `F1') addtext(Cluster, State-cohort,Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) replace
		
		reghdfe fdroa scs scs_shockvavefroap shockvavefroap `control' if h_avg_hhi_sic2==1,absorb(firmn yearn) vce(cluster incorpn#cohort )
		local coeff=_b[shockvavefroap]+_b[scs_shockvavefroap]		
		test scs_shockvavefroap+shockvavefroap=0
		local F1=r(p)
		outreg2 using "$output/Table6.xls", keep (scs scs_shockvavefroap shockvavefroap  `control') stats(coef se) ctitle(High avg_hhi_sic2) adjr2 adds(N_full, e(N_full), b2+b3,`coeff', F-stat p-value, `F1') addtext(Cluster, State-cohort,Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append	
	
***************************************************
*** Table7 The effects of SCS law adoption on tail risks
***************************************************
		use tailrisk_cohort_sample,clear
		local control = "lag_logmv lag_mlev lag_roa lag_capex lag_mb lag_sgrw" 
		qui foreach x in tail_risk_ave `control' {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}			
		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)				
		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2
		
		reghdfe tail_risk_ave scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control',absorb(firmn yearn) vce(cluster incorpn#cohort )
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table7.xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(tail_risk_ave) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append	
		
**************************************************
*** Table8 Column 1 Exclude the lobbying firms                         
**************************************************	
		use _coc_cohort_sample, clear		
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"					
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls  {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)
		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2
		reghdfe rgls scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control' if mf~=1,absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table8(1).xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append

**************************************************
*** Table8 Column 2 Control for local economic conditions
**************************************************	
        use _coc_cohort_sample, clear		
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"					
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls lag_HQlngdp_pop lag_HQgdp_growth {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)
		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2
		reghdfe rgls scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control' lag_HQlngdp_pop lag_HQgdp_growth ,absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table8(2).xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control' lag_HQlngdp_pop lag_HQgdp_growth) stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-Cohort FE, YES) dec(3) tdec(2) append		

**************************************************
*** Table8 Column 3 Use the entropy-balancing matched sample
**************************************************	
        use _coc_cohort_sample, clear		
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"					
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls lag_HQlngdp_pop lag_HQgdp_growth {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		
		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)
		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2
		
		gen relative_yr=year-cohort
		ebalance treat ubeta idvol logmv mb mlev fbias sgrw if relative_yr==0, targets(2) g(ebw1)
        bysort cohort gvkey: egen ebw2=mean(ebw1)
		reghdfe rgls scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control' [pweight=ebw2] ,absorb(firmn yearn) vce(cluster incorpn#cohort ) 
	    test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table8(3).xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1+b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append

**************************************************
*** Table8 Column 4 Alternative measure of competition
**************************************************					
        use cocsample, clear	
	    drop if avg_hhi_sic3==.
		gen scs=(year >=list_scs)
		replace scs=0 if year >=alist_scs & year <blist_scs & alist_scs~=.	
		save _rgls3,replace
		
*** setup the cohort window, [-10 10] for each event		
		foreach event_time in 1984 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007 {
		use _rgls3,replace				
		keep gvkey year avg_hhi_sic3 list_scs blist_scs state_incorp 
		keep if year==`event_time' & (list_scs==. | list_scs ==`event_time'| blist_scs==`event_time')	
		gen pref10 =year-10
		gen postf10 =year+10 if state_incorp~="NE"
		replace postf10 =year+6 if state_incorp=="NE" & `event_time'==1988
		drop list_scs blist_scs
		save _event`event_time',replace
		}
		
		use _event1984, clear
		foreach event_time in 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007{
		append using _event`event_time'
		}
		save _allevent ,replace
		
		use _allevent,clear
		by year, sort: egen median_cvari=pctile(avg_hhi_sic3), p(33)     		
	    gen h_avg_hhi_sic3=0 
	    replace h_avg_hhi_sic3=1 if avg_hhi_sic3 >= median_cvari
		gen l_avg_hhi_sic3=0 
	    replace l_avg_hhi_sic3=1 if avg_hhi_sic3 < median_cvari
		keep gvkey year pref10 postf10 h_avg_hhi_sic3 l_avg_hhi_sic3 state_incorp	
	    rename year cohort
		save _cohortevent ,replace
				
		use _rgls3, clear
		joinby gvkey state_incorp using _cohortevent
		replace cohort=. if year <pref10
		replace cohort=. if year >postf10
		keep if cohort~=.
		save _rgls4, replace
	
		use _rgls4, clear
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"
		drop if ubeta==.| idvol==.| logmv==.| mb==.| mlev==.| fbias==.| sgrw==. |ixrgls==.				
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)

		gen scs_h_avg_hhi_sic3=scs*h_avg_hhi_sic3
	    gen scs_l_avg_hhi_sic3=scs*l_avg_hhi_sic3
				
		reghdfe rgls scs_l_avg_hhi_sic3 scs_h_avg_hhi_sic3 `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic3=scs_h_avg_hhi_sic3
		local F1=r(p)
		outreg2 using "$output/Table8(4).xls", keep (scs_l_avg_hhi_sic3 scs_h_avg_hhi_sic3 `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append		

**************************************************
*** Table8 Column 5 Use Ravg (%) to proxy cost of equity
**************************************************					
        use cocsample, clear	
	    drop if ravg==.| avg_hhi_sic2==. 	
		gen scs=(year >=list_scs)
		replace scs=0 if year >=alist_scs & year <blist_scs & alist_scs~=.
		save _ravg1,replace
		
*** setup the cohort window, [-10 10] for each event		
		foreach event_time in 1984 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007{
		use _ravg1,replace					
		keep gvkey year avg_hhi_sic2 list_scs blist_scs state_incorp 	
		keep if year==`event_time' & (list_scs==. | list_scs ==`event_time'| blist_scs==`event_time')
		gen pref10 =year-10
		gen postf10 =year+10 if state_incorp~="NE"
		replace postf10 =year+6 if state_incorp=="NE" & `event_time'==1988
		drop list_scs blist_scs
		save _event`event_time',replace
		}
		
		use _event1984, clear
		foreach event_time in 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007{
		append using _event`event_time'
		}
		save _allevent ,replace
		
		use _allevent,clear
		by year, sort: egen median_cvari=pctile(avg_hhi_sic2), p(33)     		
	    gen h_avg_hhi_sic2=0 
	    replace h_avg_hhi_sic2=1 if avg_hhi_sic2 >= median_cvari
		gen l_avg_hhi_sic2=0 
	    replace l_avg_hhi_sic2=1 if avg_hhi_sic2 < median_cvari
		keep gvkey year pref10 postf10 h_avg_hhi_sic2 l_avg_hhi_sic2 state_incorp	
	    rename year cohort
		save _cohortevent ,replace

		use _ravg1, clear
		joinby gvkey state_incorp	using _cohortevent
		replace cohort=. if year <pref10
		replace cohort=. if year >postf10
		keep if cohort~=.
		save _ravg2, replace
		
		use _ravg2, clear

	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixravg"
		drop if ravg==. |ubeta==.| idvol==.| logmv==.| mb==.| mlev==.| fbias==.| sgrw==. |ixravg==.				
		qui foreach x in ravg ubeta idvol logmv mb mlev fbias sgrw ixravg {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)

		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2

		reghdfe ravg scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table8(5).xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(ravg) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append	

**************************************************
*** Table8 Column 6 Exclude firms incorporated in DE
**************************************************						
        use cocsample, clear	
	    drop if avg_hhi_sic2==. 		
		drop if state_incorp=="DE"
		gen scs=(year >=list_scs)
		replace scs=0 if year >=alist_scs & year <blist_scs & alist_scs~=.
		save _rgls5,replace
		
*** setup the cohort window, [-10 10] for each event		
		foreach event_time in 1984 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007 {
		use _rgls5,replace				
		keep gvkey year avg_hhi_sic2 list_scs blist_scs state_incorp 		
		keep if year==`event_time' & (list_scs==. | list_scs ==`event_time'| blist_scs==`event_time')
		gen pref10 =year-10
		gen postf10 =year+10 if state_incorp~="NE"
		replace postf10 =year+6 if state_incorp=="NE" & `event_time'==1988
		drop list_scs blist_scs
		save _event0`event_time',replace
		}
		
		use _event01984, clear
		foreach event_time in 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007{
		append using _event0`event_time'
		}
		save _allevent0 ,replace

		use _allevent0,clear
		by year, sort: egen median_cvari=pctile(avg_hhi_sic2), p(33)     		
	    gen h_avg_hhi_sic2=0 
	    replace h_avg_hhi_sic2=1 if avg_hhi_sic2 >= median_cvari
		gen l_avg_hhi_sic2=0 
	    replace l_avg_hhi_sic2=1 if avg_hhi_sic2 < median_cvari
		keep gvkey year pref10 postf10 h_avg_hhi_sic2 l_avg_hhi_sic2 state_incorp	
	    rename year cohort
		save _cohortevent0 ,replace
				
		use _rgls5, clear
		joinby gvkey state_incorp using _cohortevent0
		replace cohort=. if year <pref10
		replace cohort=. if year >postf10
		keep if cohort~=.
		save _rgls6, replace
		
		use _rgls6, clear
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"
		drop if ubeta==.| idvol==.| logmv==.| mb==.| mlev==.| fbias==.| sgrw==. |ixrgls==.				
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)

		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2

		reghdfe rgls scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control' ,absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table8(6).xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append
		
***************************************************	
*** Table8 Column 7 Include not-yet-treated firms as control firms	
***************************************************
		foreach event_time in 1984 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007 {
		use _rgls1,replace				
		keep gvkey year avg_hhi_sic2 list_scs blist_scs state_incorp 
		keep if year==`event_time' & (list_scs -`event_time'> 10 | list_scs ==`event_time'| blist_scs==`event_time')		//Include not-yet-treated firms as control firms in each cohort
		gen pref10 =year-10
		gen postf10 =year+10 if state_incorp~="NE"
		replace postf10 =year+6 if state_incorp=="NE" & `event_time'==1988
		drop list_scs blist_scs
		save _event`event_time',replace
		}
		
		use _event1984, clear
		foreach event_time in 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007{
		append using _event`event_time'
		}
		save _allevent ,replace
		
		use _allevent,clear
		by year, sort: egen median_cvari=pctile(avg_hhi_sic2), p(33)     		
	    gen h_avg_hhi_sic2=0 
	    replace h_avg_hhi_sic2=1 if avg_hhi_sic2 >= median_cvari
		gen l_avg_hhi_sic2=0 
	    replace l_avg_hhi_sic2=1 if avg_hhi_sic2 < median_cvari
		keep gvkey year pref10 postf10 h_avg_hhi_sic2 l_avg_hhi_sic2 state_incorp	
	    rename year cohort
		save _cohortevent ,replace
		
		use _rgls1, clear
		joinby gvkey state_incorp using _cohortevent
		replace cohort=. if year <pref10
		replace cohort=. if year >postf10
		keep if cohort~=.
		save _rgls2, replace
		
		use _rgls2, clear
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"
		drop if ubeta==.| idvol==.| logmv==.| mb==.| mlev==.| fbias==.| sgrw==. |ixrgls==.				
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)

		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2
		
		reghdfe rgls scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table8(7).xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append		
			
**************************************************
*** Table8 Column 8  Use Sun and Abraham's (2021) estimator                                     
**************************************************	
* note: Sun and Abraham's (2021) estimator & Borusyak et al.'s (2024) estimator both require the grouping variable to be fixed within the whole sample, instead of within each cohort         
		use _cohortevent_rgls0,clear
		keep if list_scs~=.
		keep gvkey h_avg_hhi_sic2 
		duplicates drop
		rename  h_avg_hhi_sic2 high_hhi  
		save _cohortevent_rgls00,replace		
		
		use _rgls1, clear
		merge n:1 gvkey using _cohortevent_rgls00,keep(1 3) nogen
		replace high_hhi=0 if list_scs==.
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"
		drop if ubeta==.| idvol==.| logmv==.| mb==.| mlev==.| fbias==.| sgrw==. |ixrgls==.				
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx                                                              
		 drop xx
		}		
		egen incorpn = group(state_incorp)  
		gen ry = year - list_scs		
		gen never_treat = (list_scs == .)
		gen scs_high_hhi=scs*high_hhi
		gen scs_low_hhi=scs*(1-high_hhi)

log using "$output/Table8(8)_note", text  replace			
		eventstudyinteract rgls scs_high_hhi scs_low_hhi , cohort(list_scs) control_cohort(never_treat) covariates(ubeta idvol logmv mb mlev fbias sgrw ixrgls) absorb(i.gvkey i.year) vce(cluster incorpn)
		matrix b = e(b_iw)
        matrix V = e(V_iw)
        ereturn post b V
		test scs_high_hhi=scs_low_hhi 
		local F1=r(p)
		 outreg2 using "$output/Table8(8).xls", stats(coef se) ctitle(rgls) adds(b1=b2 F-stat p-value, `F1') addtext(Cluster, HQ State, Firm FE, YES, Year FE, YES, Controls, YES) dec(3) tdec(2) append		 
log close

**************************************************		
*** Table8 Column 9 Use Borusyak et al.'s (2024) estimator	
**************************************************		
* note: This code will produce the ATT for high_hhi==0 (tau_0) and high_hhi==1 (tau_1).	
	    did_imputation rgls gvkey year list_scs, controls(`control') fe(gvkey year) autosample tol(0.0001) maxit(1000) cluster(incorpn) delta(1)  hetby(high_hhi)
		test tau_0=tau_1
		local F1=r(p)
        outreg2 using "$output/Table8(9).xls", stats(coef se) ctitle(rgls) adds(N_full, e(N), b1=b2 F-stat p-value, `F1') addtext(Cluster, State, Firm FE, YES, Year FE, YES) dec(3) tdec(2) append
		
		reghdfe rgls `control' if scs==0&cannot_impute==0 ,absorb(gvkey year) vce(cluster incorpn)
		outreg2 using "$output/Table8(9).xls", stats(coef se) ctitle(rgls) adds(N_full, e(N)) adjr2 addtext(Cluster, State, Firm FE, YES, Year FE, YES) dec(3) tdec(2) append		

**************************************************
*** Table9A The SCS law and the probability of being hostilely acquired
**************************************************		
		use _rgls1,clear
	    local other_law="antilaw2 firstlaw"
	    local control_tko = "roa_indadj logmv mlev_indadj mb sgrw ppe dIndhstfirm_pub"
		drop if f_dhstltko_target==. |roa_indadj==. | logmv==. | mlev_indadj==. | mb==. | sgrw==. | ppe==. | dIndhstfirm_pub==. 
				
		qui foreach x in roa_indadj logmv mlev_indadj mb sgrw ppe {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		
		egen incorpn = group(state_incorp)  

		reghdfe f_dhstltko_target scs `other_law',absorb(gvkey year) vce(cluster incorpn) 
		outreg2 using "$output/Table9A.xls", keep (scs `other_law') stats(coef se) ctitle(f_dhstltko_target) adjr2 adds(N_full, e(N_full)) addtext(Cluster, State, Firm FE, YES, Year FE, YES) dec(4) pdec(3) append
						
		reghdfe f_dhstltko_target scs `other_law' `control_tko',absorb(gvkey year) vce(cluster incorpn) 
		outreg2 using "$output/Table9A.xls", keep (scs `control_tko' `other_law') stats(coef se) ctitle(f_dhstltko_target) adjr2 adds(N_full, e(N_full)) addtext(Cluster, State, Firm FE, YES, Year FE, YES) dec(4) pdec(3) append		

**************************************************
*** Table9B Column 1 The role of hostile takeover threat before the SCS law
**************************************************	
		use cocsample, clear
	    drop if avg_hhi_sic2==. | lag_hstltko_prob_indp==.
		gen scs=(year >=list_scs)
		replace scs=0 if year >=alist_scs & year <blist_scs & alist_scs~=.
		save _rgls7,replace
		
*** setup the cohort window, [-10 10] for each event		
		foreach event_time in 1984 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007 {
		use _rgls7,replace				
		keep gvkey year avg_hhi_sic2 list_scs blist_scs state_incorp lag_hstltko_prob_indp
		keep if year==`event_time' & (list_scs==. | list_scs ==`event_time'| blist_scs==`event_time')
		gen pref10 =year-10
		gen postf10 =year+10 if state_incorp~="NE"
		replace postf10 =year+6 if state_incorp=="NE" & `event_time'==1988
		drop list_scs blist_scs
		save _event`event_time',replace
		}
		
		use _event1984, clear
		foreach event_time in 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007{
		append using _event`event_time'
		}
		save _allevent ,replace
		
		use _allevent,clear
		by year, sort: egen median_cvari1=pctile(avg_hhi_sic2), p(33)     		
	    gen h_avg_hhi_sic2=0 
	    replace h_avg_hhi_sic2=1 if avg_hhi_sic2 >= median_cvari1
		gen l_avg_hhi_sic2=0 
	    replace l_avg_hhi_sic2=1 if avg_hhi_sic2 < median_cvari1
		
		by h_avg_hhi_sic2 year , sort: egen median_cvari2=pctile(lag_hstltko_prob_indp), p(50)     
	    gen h_lag_hstltko_prob_indp=0 
	    replace h_lag_hstltko_prob_indp=1 if lag_hstltko_prob_indp >= median_cvari2
		gen l_lag_hstltko_prob_indp=0 
	    replace l_lag_hstltko_prob_indp=1 if lag_hstltko_prob_indp < median_cvari2
		replace  h_lag_hstltko_prob_indp=. if lag_hstltko_prob_indp==.
		replace  l_lag_hstltko_prob_indp=. if lag_hstltko_prob_indp==.
		
		keep gvkey year pref10 postf10 h_avg_hhi_sic2 l_avg_hhi_sic2 h_lag_hstltko_prob_indp l_lag_hstltko_prob_indp state_incorp	
	    rename year cohort
		save _cohortevent ,replace
			
		use _rgls7, clear
		joinby gvkey state_incorp using _cohortevent
		replace cohort=. if year <pref10
		replace cohort=. if year >postf10
		keep if cohort~=.
		save _rgls8, replace
			
		use _rgls8, clear	
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"	
		drop if ubeta==.| idvol==.| logmv==.| mb==.| mlev==.| fbias==.| sgrw==. |ixrgls==.				
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)
				
		gen scs_hHHI_htko=scs*h_avg_hhi_sic2*h_lag_hstltko_prob_indp
		gen scs_hHHI_ltko=scs*h_avg_hhi_sic2*l_lag_hstltko_prob_indp
		gen scs_lHHI_htko=scs*l_avg_hhi_sic2*h_lag_hstltko_prob_indp
		gen scs_lHHI_ltko=scs*l_avg_hhi_sic2*l_lag_hstltko_prob_indp
		
		reghdfe rgls scs_lHHI_htko scs_lHHI_ltko scs_hHHI_htko scs_hHHI_ltko `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		
		test scs_lHHI_htko=scs_lHHI_ltko
		local F1=r(p)

		test scs_hHHI_htko=scs_hHHI_ltko
		local F2=r(p)
		
		outreg2 using "$output/Table9B(1).xls", keep (scs_lHHI_htko scs_lHHI_ltko scs_hHHI_htko scs_hHHI_ltko `control') stats(coef se) ctitle(rgls lag_hstltko_prob_indp) adjr2 adds(N_full, e(N_full),b1=b2 p-value, `F1', b3=b4 p-value, `F2') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append		
			
***************************************************
*** Table9B Column 2 Control for other takeover laws 
***************************************************
        use _coc_cohort_sample, clear		
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"					
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls  {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)
		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2
		
		reghdfe rgls scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 firstlaw antilaw2 `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table9B(2).xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 firstlaw antilaw2 `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append		

**************************************************
*** Table10 The role of options to bypass the SCS law
**************************************************	
*** Panel A: Redo Table 4 with three states in the treatment group (PA,GA,TN)					
        use cocsample, clear
		keep if list_scs==. | state_incorp=="PA"|state_incorp=="GA"|state_incorp=="TN"		
	    drop if avg_hhi_sic2==. 
        gen scs=(year >=list_scs)
		replace scs=0 if year >=alist_scs & year <blist_scs & alist_scs~=.
		save _rgls9,replace
		
*** setup the cohort window, [-10 10] for each event		
		foreach event_time in 1988 1989 1990 {
		use _rgls9,replace				
		keep gvkey year avg_hhi_sic2 list_scs blist_scs state_incorp 
		keep if year==`event_time' & (list_scs==. | list_scs ==`event_time'| blist_scs==`event_time')	
		gen pref10 =year-10
		gen postf10 =year+10 if state_incorp~="NE"
		replace postf10 =year+6 if state_incorp=="NE" & `event_time'==1988
		drop list_scs blist_scs
		save _event`event_time',replace
		}
		
		use _event1988, clear
		foreach event_time in 1989 1990 {
		append using _event`event_time'
		}
		save _allevent ,replace
		
		use _allevent,clear
		by year, sort: egen median_cvari=pctile(avg_hhi_sic2), p(33)     		
	    gen h_avg_hhi_sic2=0 
	    replace h_avg_hhi_sic2=1 if avg_hhi_sic2 >= median_cvari
		gen l_avg_hhi_sic2=0 
	    replace l_avg_hhi_sic2=1 if avg_hhi_sic2 < median_cvari
		keep gvkey year pref10 postf10 h_avg_hhi_sic2 l_avg_hhi_sic2 	state_incorp	
	    rename year cohort
		save _cohortevent ,replace
	
		use _rgls9, clear
		joinby gvkey state_incorp using _cohortevent
		replace cohort=. if year <pref10
		replace cohort=. if year >postf10
		keep if cohort~=.
		save _rgls10, replace
		
		use _rgls10, clear	
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"
		drop if ubeta==.| idvol==.| logmv==.| mb==.| mlev==.| fbias==.| sgrw==. |ixrgls==.				
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)

		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2
		
		reghdfe rgls scs `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		outreg2 using "$output/Table10.xls", keep (scs `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full)) addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) replace

		reghdfe rgls scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table10.xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append

*** Panel B: Redo Table 4 with the remaining states in the treatment group except for (PA,GA,TN)      						
        use cocsample, clear	
		drop if state_incorp=="PA" | state_incorp=="GA" | state_incorp=="TN"		
	    drop if avg_hhi_sic2==. 
        gen scs=(year >=list_scs)
		replace scs=0 if year >=alist_scs & year <blist_scs & alist_scs~=.
		save _rgls11,replace
		
*** setup the cohort window, [-10 10] for each event		
		foreach event_time in 1984 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007 {
		use _rgls11,replace				
		keep gvkey year avg_hhi_sic2 list_scs blist_scs state_incorp 
		keep if year==`event_time' & (list_scs==. | list_scs ==`event_time'| blist_scs==`event_time')
		gen pref10 =year-10
		gen postf10 =year+10 if state_incorp~="NE"
		replace postf10 =year+6 if state_incorp=="NE" & `event_time'==1988
		drop list_scs blist_scs
		save _event`event_time',replace
		}
		
		use _event1984, clear
		foreach event_time in 1985 1986 1987 1988 1989 1990 1991 1993 1998 1999 2006 2007 {
		append using _event`event_time'
		}
		save _allevent ,replace
		
		use _allevent,clear
		by year, sort: egen median_cvari=pctile(avg_hhi_sic2), p(33)     		
	    gen h_avg_hhi_sic2=0 
	    replace h_avg_hhi_sic2=1 if avg_hhi_sic2 >= median_cvari
		gen l_avg_hhi_sic2=0 
	    replace l_avg_hhi_sic2=1 if avg_hhi_sic2 < median_cvari
		keep gvkey year pref10 postf10 h_avg_hhi_sic2 l_avg_hhi_sic2 state_incorp	
	    rename year cohort
		save _cohortevent ,replace
	
		use _rgls11, clear
		joinby gvkey state_incorp using _cohortevent
		replace cohort=. if year <pref10
		replace cohort=. if year >postf10
		keep if cohort~=.
		save _rgls12, replace
		
		use _rgls12, clear	
	    local control = "ubeta idvol logmv mb mlev fbias sgrw ixrgls"
		drop if ubeta==.| idvol==.| logmv==.| mb==.| mlev==.| fbias==.| sgrw==. |ixrgls==.				
		qui foreach x in rgls ubeta idvol logmv mb mlev fbias sgrw ixrgls {
		 winsor `x', g(xx) p(0.01)
		 replace `x'=xx
		 drop xx
		}		

		egen incorpn = group(state_incorp)  
		egen firmn=group(gvkey cohort)
		egen yearn=group(year cohort)

		gen scs_h_avg_hhi_sic2=scs*h_avg_hhi_sic2
	    gen scs_l_avg_hhi_sic2=scs*l_avg_hhi_sic2
		
		reghdfe rgls scs `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		outreg2 using "$output/Table10.xls", keep (scs `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full)) addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append

		reghdfe rgls scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control',absorb(firmn yearn) vce(cluster incorpn#cohort ) 
		test scs_l_avg_hhi_sic2=scs_h_avg_hhi_sic2
		local F1=r(p)
		outreg2 using "$output/Table10.xls", keep (scs_l_avg_hhi_sic2 scs_h_avg_hhi_sic2 `control') stats(coef se) ctitle(rgls) adjr2 adds(N_full, e(N_full), b1=b2 F-stat p-value, `F1') addtext(Cluster, State-Cohort, Firm-cohort FE, YES, Year-cohort FE, YES) dec(3) tdec(2) append			

**************************************************
**************************************************

***** Erase process data *****		

local list : dir . files "_*.dta"
foreach f of local list {
    erase "`f'"
}

foreach x in Table2 Table3 Table4_dy Table5 Table6 Table7 Table8(1) Table8(2) Table8(3) Table8(4) Table8(5) Table8(6) Table8(7) Table8(8) Table8(9) Table9A Table9B(1) Table9B(2) Table10 {
    erase "$output/`x'.txt"
}

***** END OF DO-FILE *****		
			